package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/login1.do")
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 处理GET请求
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户输入的用户名和兴趣爱好
        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String[] favor = request.getParameterValues("interest");

        // 将兴趣爱好数组转换成字符串
        String favorString = "";
        if (favor != null && favor.length > 0) {
            favorString = String.join(",", favor);
        }

        // 将用户名和兴趣爱好信息存储到请求属性中
        request.setAttribute("username", username);
        request.setAttribute("interest", favorString);

        // 转发到result.jsp页面
        request.getRequestDispatcher("/work1/result.jsp").forward(request, response);
    }
}
